package com.mlh.handler.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mlh.util.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 缪林辉
 * @date 2024/3/16 11:30
 * @DESCRIPTION
 */
//自动补全属性 createTime createBy
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //新增
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = null;
        // try {
            //利用工具类获取用户id
            userId = SecurityUtils.getUserId();
        // } catch (Exception e) {
        //     e.printStackTrace();
        //     userId = -1L;//表示是自己创建
        // }
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("createBy",userId , metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", userId, metaObject);
    }
    //更新
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName(" ", SecurityUtils.getUserId(), metaObject);
    }
}
